﻿using System;
using System.Collections.Generic;
using System.Linq;
using Fit2ModelsFluent;
using Fit2InfrastructureFluent;
using NHibernate;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace FIt2IntegrationFluentTests
{
    [TestFixture]
    public class QuestionRepositoryTester
    {

        [Test]
        public void When_saving_updated_should_write_to_database()
        {
            var config = new DataConfig();
            config.PerformStartup();
            config.StartSession();

            var question = new Question
            {
                 InsertBy ="David",
                  InsertDate = System.DateTime.Now,
                   QuestionName = "Question Test1",
                    QuestionText = "What is Test1",
                      RecordStatus = 1,
                       SortOrder = 1,
                        UpdateBy = "David",
                         UpdateDate = System.DateTime.Now

            };

            var repository = new QuestionRepository();
            repository.Save(question);
            Assert.That(question, Is.Not.Null);
            Assert.That(question.QuestionId, Is.Not.Null);

            //Update
            question.QuestionText = "Was this Updated?";
            repository.Save(question);

            //Retrieved Updated Question
            Question updatedQuestion = repository.GetQuestion(question.QuestionId);
            Assert.That(updatedQuestion.QuestionText,Is.EqualTo("Was this Updated?"));


          
        }

        [Test]
        public void When_saving_updated_using_Transaction_should_write_to_database()
        {
            var config = new DataConfig();
            config.PerformStartup();
            config.StartSession();

            var question = new Question
            {
                InsertBy = "David",
                InsertDate = System.DateTime.Now,
                QuestionName = "Question Test1",
                QuestionText = "What is Test1",
                RecordStatus = 1,
                SortOrder = 1,
                UpdateBy = "David",
                UpdateDate = System.DateTime.Now

            };

            var repository = new QuestionRepository();
            repository.SaveUsingTransaction(question);
            Assert.That(question, Is.Not.Null);
            Assert.That(question.QuestionId, Is.Not.Null);

            //Update
            question.QuestionText = "Was this Updated?";
            repository.SaveUsingTransaction(question);

            //Retrieved Updated Question
            Question updatedQuestion = repository.GetQuestion(question.QuestionId);
            Assert.That(updatedQuestion.QuestionText, Is.EqualTo("Was this Updated?"));



        }


        [Test]
        public void When_saving_updated_should_write_to_database_added_answers()
        {
            var config = new DataConfig();
            config.PerformStartup();
            config.StartSession();

            var answer = new Answer
            {
                AnswerName = "Answer1",
                AnswerText = "This is the answer to Answer1",
                InsertBy = "David",
                InsertDate = System.DateTime.Now,
                RecordStatus = 1,
                SortOrder = 1,
                UpdateBy = "David",
                UpdateDate = System.DateTime.Now
            };

            var answerRepository = new AnswerRepository();
            answerRepository.Save(answer);
            Assert.That(answer, Is.Not.Null);
            Assert.That(answer.AnswerId, Is.Not.Null);


            var question = new Question
            {
                InsertBy = "David",
                InsertDate = System.DateTime.Now,
                QuestionName = "Question Test1",
                QuestionText = "What is Test1",
                RecordStatus = 1,
                SortOrder = 1,
                UpdateBy = "David",
                UpdateDate = System.DateTime.Now,
                Answers = new List<Answer>(){answer}

            };

            var repository = new QuestionRepository();
            repository.Save(question);
            Assert.That(question, Is.Not.Null);
            Assert.That(question.QuestionId, Is.Not.Null);

            //Update
            question.QuestionText = "Was this Updated?";
            repository.Save(question);

            //Retrieved Updated Question
            Question updatedQuestion = repository.GetQuestion(question.QuestionId);
            Assert.That(updatedQuestion.QuestionText, Is.EqualTo("Was this Updated?"));



        }
    }
}
